package com.dodock.android.banglapapers.ui;

import android.os.Bundle;
import android.view.Gravity;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dodock.android.banglapapers.BanglaNews;
import com.dodock.android.banglapapers.MyApplication;
import com.dodock.android.banglapapers.R;
import com.dodock.android.banglapapers.imageLoader.ImageLoader;
import com.dodock.android.banglapapers.widgets.HorizontalPager;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;
import com.loopj.android.image.SmartImageView;

public class GalleryActivity extends BaseActivity{
	
	HorizontalPager realViewSwitcher;
	int totalNo;
	ImageLoader imageLoader;
	
	private Tracker mGaTracker;
	private GoogleAnalytics mGaInstance;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
				
		//Google Analytics
		try{
		    mGaInstance = GoogleAnalytics.getInstance(this);
		    mGaTracker = mGaInstance.getTracker(getResources().getString(R.string.ga_trackingId));
		}
		catch (Exception e) {
		}
		//--Google Analytics
		
		
		setContentView(R.layout.gallery);
		totalNo = NewsDetailsActivity.newsDetails.getGalleryUrlList().size();
		setIntoView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		com.facebook.Settings.publishInstallAsync(this, MyApplication.fb_app_id);
	}
	
	
	@Override
	protected void onStart() {
		super.onStart();
		try {
			mGaTracker.sendView(String.format("/%s/gallery/%s", NewsDetailsActivity.newsDetails.getPaperId(), NewsDetailsActivity.newsDetails.getId()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	private void setIntoView() {
		imageLoader = new ImageLoader(this);
		realViewSwitcher = (HorizontalPager) findViewById(R.id.real_view_switcher);

		for (int i = 0; i < NewsDetailsActivity.newsDetails.getGalleryUrlList().size(); i++) {
			//OLDIMAGE
			//ImageView imageView = new ImageView(GalleryActivity.this);
			//LayoutParams lp = null;
			//if(BanglaNews.SCREEN_WIDTH < BanglaNews.SCREEN_HEIGHT){
			//	lp = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
			//			BanglaNews.SCREEN_WIDTH);	
			//}else{
			//	lp = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
			//			BanglaNews.SCREEN_HEIGHT);
			//}		
			//lp.gravity = Gravity.CENTER;
			//imageView.setLayoutParams(lp);
			//imageLoader.DisplayImage(NewsDetailsActivity.newsDetails.getGalleryUrlList().elementAt(i),
			//		R.drawable.dodocklogo, imageView, false, null);
			//realViewSwitcher.addView(imageView);
			//--OLDIMAGE
			
			
			//NEWIMAGE
			SmartImageView imageView = new SmartImageView(GalleryActivity.this);
			LayoutParams lp = null;
			if(BanglaNews.SCREEN_WIDTH < BanglaNews.SCREEN_HEIGHT){
				lp = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
						BanglaNews.SCREEN_WIDTH);	
			}
			else{
				lp = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
						BanglaNews.SCREEN_HEIGHT);
			}		
			lp.gravity = Gravity.CENTER;
			imageView.setLayoutParams(lp);
			imageView.setScaleType(ScaleType.CENTER_INSIDE);
			imageView.setImageUrl(NewsDetailsActivity.newsDetails.getGalleryUrlList().elementAt(i));
			realViewSwitcher.addView(imageView);
			//--NEWIMAGE
		}
		
		realViewSwitcher.setOnScreenSwitchListener(onScreenSwitchListener);
		realViewSwitcher.setCurrentScreen( 0, true);
	}

	private final HorizontalPager.OnScreenSwitchListener onScreenSwitchListener = new HorizontalPager.OnScreenSwitchListener() {
		public void onScreenSwitched(final int screen) {
			TextView title = (TextView) findViewById(R.id.title);
			title.setText((screen + 1) + " of " + (totalNo));
		}
	};
}
